package com.example.saifly.ReceiverAndService;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;

import com.example.common.Application.MyApplication;
import com.example.saifly.mainPage.plan.AlarmRemindActivity;

import java.util.Calendar;

import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;

public class AlarmReceiver extends BroadcastReceiver {
    private SharedPreferences sharedPreferences = MyApplication.getContext().getSharedPreferences("config", Context.MODE_PRIVATE);

    @Override
    public void onReceive(Context context, Intent intent) {
//        Toast.makeText(context,"收到广播", Toast.LENGTH_LONG).show();
        //执行我们写的定时提醒服务
        SetAlarm(context);          //定时提醒执行计划
        SetEvaluation(context);    //每晚后台定时执行评估算法
    }

    private void SetEvaluation(Context context) {
        //如果没有录入医嘱则不设置闹钟

        AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        long systemTime = System.currentTimeMillis();

        Intent intent = new Intent(context, EvaluationService.class);

        //生成一个时间设置器
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(systemTime);

        calendar.set(Calendar.HOUR_OF_DAY,23);
        calendar.set(Calendar.MINUTE,30);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND, 0);
        long selectTime = calendar.getTimeInMillis();
        //如果当前时间大于设置的时间，那么就设置第二天的定时服务
        if(systemTime > selectTime){
            calendar.add(Calendar.DAY_OF_MONTH,1);
            selectTime = calendar.getTimeInMillis();
        }
        PendingIntent pi = PendingIntent.getService(context, 2, intent, FLAG_UPDATE_CURRENT);
        manager.set(AlarmManager.RTC_WAKEUP, selectTime, pi);//开启提醒
    }

    private void SetAlarm(Context context) {
        if(sharedPreferences.getString("nextPlanTime","暂无").equals("暂无")) return ;

        AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);//获取AlarmManager实例
        long systemTime = System.currentTimeMillis();
        //生成一个时间设置器
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(systemTime);

        //如果今日计划时间没有全部过时。 设置单次闹钟
        if(sharedPreferences.getString("nextPlanTime","00").indexOf("明") == -1){
            calendar.set(Calendar.HOUR_OF_DAY,Integer.valueOf(sharedPreferences.getString("nextPlanTimeHour","12")));
            calendar.set(Calendar.MINUTE,Integer.valueOf(sharedPreferences.getString("nextPlanTimeMinute","00")));
        }
        else {
            calendar.set(Calendar.HOUR_OF_DAY,Integer.valueOf(sharedPreferences.getString("tomorrowFirstPlanTimeHour","12")));
            calendar.set(Calendar.MINUTE,Integer.valueOf(sharedPreferences.getString("tomorrowFirstPlanTimeMinute","00")));
            calendar.add(Calendar.DAY_OF_MONTH,1);
        }
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND, 0);
        long selectTime = calendar.getTimeInMillis();
        Log.d("123", "Hour: "+calendar.get(Calendar.HOUR_OF_DAY)+" Minute: "+calendar.get(Calendar.MINUTE));

        Intent myIntent = new Intent(context, AlarmRemindActivity.class);
//        myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);     //必须设置标志
        PendingIntent pi = PendingIntent.getActivity(context, 1, myIntent, FLAG_UPDATE_CURRENT);
        manager.set(AlarmManager.RTC_WAKEUP, selectTime, pi);//开启提醒
    }
}
